package org.one.stone.soup.grfxML.plugin.function;

import org.one.stone.soup.grfxML.DataPoint;
import org.one.stone.soup.grfxML.DataSize;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.math.IntMath;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class ArrowControl extends SimplePlugin {

	public static final int ARG_START = 0;
	public static final int ARG_END = 1;
	public static final int ARG_ROTATION = 2;
	public static final int ARG_POSITION = 3;

	DataPoint start = new DataPoint();
	DataPoint end = new DataPoint();
	DataSize rotation = new DataSize();
	DataPoint position = new DataPoint();

	private int count = 0;
/**
 * StateInverter constructor comment.
 */
public ArrowControl(GrfxMLEngine engine){
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
}
/**
 * process method comment.
 */
public void process()
{
 try{
 	count++;

	if( start.hasChanged() || end.hasChanged() || count==10)
	{
		int x = end.getValueX()-start.getValueX();
		int y = end.getValueY()-start.getValueY();
		int angle = IntMath.angle(x,y);

		rotation.setValue( angle,this );
		position.setValueX( start.getValueX()+(x/2),this );
		position.setValueY( start.getValueY()+(y/2),this );

		count = 0;
	}
 }catch(LockException le){}
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{

	start = grfxMLCaster.cast(start,getArg(start,ARG_START,store));
	end = grfxMLCaster.cast(end,getArg(end,ARG_END,store));
	rotation = grfxMLCaster.cast(rotation,getArg(rotation,ARG_ROTATION,store));
	position = grfxMLCaster.cast(position,getArg(position,ARG_POSITION,store));

}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{

	start = grfxMLCaster.replace(start,oldObj,newObj);
	end = grfxMLCaster.replace(end,oldObj,newObj);
	rotation = grfxMLCaster.replace(rotation,oldObj,newObj);
	position = grfxMLCaster.replace(position,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
